package org.xhy.bytebuddy;

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.Callable;

public class AnnotationMethodInterceptor {
    @RuntimeType
    public static Object intercept(
            @AllArguments Object[] args,   // 获取方法参数
            @Origin Method method,         // 获取方法对象
            @SuperCall Callable<?> zuper   // 调用原始方法
    ) throws Exception {
        // 方法调用前的逻辑
        System.out.println("Before annotation method: " + method.getName() + ", with arguments: " + Arrays.toString(args));

        // 调用原方法
        Object result = zuper.call();

        // 方法调用后的逻辑
        System.out.println("After annotation method: " + method.getName() + ", returned: " + result);

        return result;
    }
}
